package com.maodou.core.model

import com.squareup.moshi.JsonClass

/**
 * 用户信息数据模型
 */
@JsonClass(generateAdapter = true)
data class UserInfo(
    val id: String,
    val nickName: String? = null,
    val email: String? = null,
    val phoneNumber: String? = null,
    val avatar: String? = null,
    val role: String? = null,
    val permissions: List<String> = emptyList(),
    val department: String? = null,
    val position: String? = null,
    val token: String? = null,
    val apiKey: String? = null,
    val status: String? = null, // ACTIVE: 正常, 0: 禁用
    val createTime: Long? = null,
    val updateTime: Long? = null,
    val lastLoginTime: Long? = null,
    val extra: Map<String, Any> = emptyMap() // 扩展字段
) {
    /**
     * 获取显示名称
     */
    fun getDisplayName(): String {
        return nickName?.ifEmpty { email ?: phoneNumber ?: id } ?: id
    }
    
    /**
     * 检查用户是否激活
     */
    fun isActive(): Boolean {
        return status == "ACTIVE"
    }
    
    /**
     * 检查是否有指定权限
     */
    fun hasPermission(permission: String): Boolean {
        return permissions.contains(permission)
    }
    
    /**
     * 检查是否有任意一个权限
     */
    fun hasAnyPermission(vararg permissions: String): Boolean {
        return permissions.any { this.permissions.contains(it) }
    }
    
    /**
     * 检查是否有所有权限
     */
    fun hasAllPermissions(vararg permissions: String): Boolean {
        return permissions.all { this.permissions.contains(it) }
    }
    
    /**
     * 获取扩展字段值
     */
    fun getExtraValue(key: String): Any? {
        return extra[key]
    }
    
    /**
     * 获取扩展字段字符串值
     */
    fun getExtraString(key: String, defaultValue: String = ""): String {
        return extra[key] as? String ?: defaultValue
    }
    
    /**
     * 获取扩展字段整数值
     */
    fun getExtraInt(key: String, defaultValue: Int = 0): Int {
        return extra[key] as? Int ?: defaultValue
    }
    
    /**
     * 获取扩展字段布尔值
     */
    fun getExtraBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return extra[key] as? Boolean ?: defaultValue
    }
}